/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package main.java.cn.webrx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * <p>Project: jse2204 - InputStream1
 * <p>Powered by webrx On 2022-09-22 10:03:14
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class InputStream1 {

    public static void main(String[] args) {

        //实现文件复制
        try(var a = new FileInputStream("c:/abc/g1.jpg");var b=new FileOutputStream("c:/abc/g2.jpg")) {
            int len = 0;
            while((len = a.read())!=-1){
                b.write(len);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        try(FileInputStream fis = new FileInputStream("c:/abc/g1.jpg")){
           byte[] buf = new byte[10];
           fis.skip(fis.available());
           int len = fis.read(buf);
           System.out.println(Arrays.toString(buf));
            System.out.println(len); //len 为-1读完
        }catch(Exception e){
        }


        try(FileInputStream fis = new FileInputStream("c:/abc/g1.jpg")){
            for (int i = 0; i < 10; i++) {
                int t = fis.read();
                System.out.printf("%03d ",t);
            }
        }catch(Exception e){
        }

        System.out.println();
        try(FileInputStream fis = new FileInputStream("c:/abc/x.png")){
            for (int i = 0; i < 10; i++) {
                int t = fis.read();
                System.out.printf("%03d ",t);
            }
        }catch(Exception e){
        }
    }


    public static void test(String[] args) {
        try(FileInputStream fis = new FileInputStream("c:/abc/g1.jpg")){
            int i = fis.read();
            System.out.println(i);

            fis.skip(2000);
            i = fis.read();
            System.out.println(i);
        }catch(Exception e){
        }

        try(FileInputStream fis = new FileInputStream("c:/abc/x.png")){
            int i = fis.read();
            System.out.println(i);
            fis.skip(2000);
            i = fis.read();
            System.out.println(i);
        }catch(Exception e){

        }
    }

}
